package com.jinzhi.eventresource.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.eventresource.dao.EventresourceAllocationsDao;
import com.jinzhi.eventresource.main.EventresourceAllocationsDO;
import com.jinzhi.eventresource.service.EventresourceAllocationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.common.base.CoreServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <pre>
 * 赛项资源模块子节点配置表
 * </pre>
 * <small> 2024-04-03 14:39:55 | lance</small>
 */
@Service
public class EventresourceAllocationsServiceImpl extends CoreServiceImpl<EventresourceAllocationsDao, EventresourceAllocationsDO> implements EventresourceAllocationsService {

    @Autowired
    private EventresourceAllocationsDao allocationsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGroup(EventresourceAllocationsDO eventresourceAllocationsDO) {
        Wrapper<EventresourceAllocationsDO> wrapper = new EntityWrapper<EventresourceAllocationsDO>()
                .eq("allocation_id", eventresourceAllocationsDO.getAllocationId());
                // 目录无组别
//                .eq("node_type", 0);
        List<EventresourceAllocationsDO> allocationsDOList = allocationsMapper.selectList(wrapper);

        for (String parentId : eventresourceAllocationsDO.getAllocationsIds()) {
            List<EventresourceAllocationsDO> nodeList = getChildren(Long.valueOf(parentId), allocationsDOList);
            EventresourceAllocationsDO selfAllcatonsDO = allocationsMapper.selectById(parentId);
            if (Objects.equals(selfAllcatonsDO.getNodeType(), 0)) {
                // 非目录自己也修改
                nodeList.add(selfAllcatonsDO);
            }
            nodeList.forEach(e -> {
                e.setGroupType(eventresourceAllocationsDO.getGroupType());
                allocationsMapper.updateById(e);
            });

        }
        return Boolean.TRUE;
    }

    @Override
    public List<EventresourceAllocationsDO> selectNoneGroupType() {
        Wrapper<EventresourceAllocationsDO> wrapper = new EntityWrapper<EventresourceAllocationsDO>()
                .eq("node_type", 0)
                .isNull("group_type");
        return allocationsMapper.selectList(wrapper);
    }

    //根据父节点获取全部子节点
    public static List<EventresourceAllocationsDO> getChildren(Long pid, List<EventresourceAllocationsDO> nodeList) {
        List<EventresourceAllocationsDO> resultList = new ArrayList<>();
        if (null != nodeList && nodeList.size() > 0) {
            try {
                for (EventresourceAllocationsDO layer : nodeList) {
                    System.out.println(Objects.equals(pid, layer.getParentId()));
                    if (Objects.equals(pid, layer.getParentId())) {
                        //添加子级节点
                        resultList.add(layer);
                        //递归获取深层节点
                        resultList.addAll(getChildren(layer.getId(), nodeList));
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return resultList;
    }
}
